import { createRouter, createWebHistory, Router } from 'vue-router';
import { useUserStore } from '../Store/modules/userinfo/UserInfo'
import Visualization from "../Views/Visualization/Visualization.vue";
import {useTabbarStore} from "../Store/modules/tabber";


//路由懒加载
const ResourceShareView = () => import("../Views/ResourceShare/ResourceShareView.vue");
const ResourceShareUploaderView = () => import("../Views/ResourceShare/ResourceShareUploaderView.vue");
const ReasourceShareXQView = () => import("../Views/ResourceShare/ReasourceShareXQView.vue");
const VideoView = () => import("../Views/Video/VideoView.vue");
const IndexView = () => import("../Views/SQYR/IndexView.vue");
const YRCGView = () => import("../Views/YRCG/YRCGView.vue");
const YRCGXQView = () => import("../Views/YRCG/YRCGXQView.vue");
const JSJLView = () => import("../Views/JSJL/JSJLView.vue");
const UserInfoView = () => import("../Views/UserInfo/UserInfoView.vue");
const UserInfoSetting = () => import("../Views/UserInfo/other/UserInfoSetting.vue");
const MMXGView = () => import("../Views/UserInfo/other/MMXGView.vue");
const CqustView = () => import("../Components/CqustHome.vue");
const AddTalk = () => import("../Views/JSJL/AddTalk.vue")
const AuthView = () => import("../Components/AuthView.vue")
const editor = () => import("../Views/UserInfo/other/editor.vue")
const ClassMangerView = () => import("../Views/UserInfo/Class/ClassMangerView.vue")
const ClassManger = () => import("../Views/UserInfo/Class/ClassManger.vue")

const JSJLXQView = () => import("../Views/JSJL/JSJLXQView.vue")
const AddPatrol = () => import("../Views/SQYR/UtilsPage/Patrol.vue")
const AddClass = () => import("../Views/SQYR/UtilsPage/XClass.vue")
const Xhome = () => import("../Views/SQYR/UtilsPage/Xhome.vue")
const AI = () => import("../Views/AIView/Room-chat.vue")
// 岚精灵测试页面，勿删！！！
const test = () => import("../Components/ljl_test_model/nav_top.vue")
const visualization = () => import("../Views/Visualization/Visualization.vue")
const visualization2 = () => import("../Views/Visualization/Visualization2.vue")
const sjksh = () => import("../Views/Visualization/sjksh.vue")
const testview = () => import("../Views/Test/TestView.vue")

const xclass = () => import("../Views/RWFB/Xclass.vue")
const xqs = () => import("../Views/RWFB/Xqs.vue")
const rwfb = () => import("../Views/RWFB/rwfb.vue")
const jsyrhome = () => import("../Views/JSYR/jsyrhome.vue")
const addjsyr = () => import("../Views/JSYR/addjsyr.vue")
const addjsjd = () => import("../Views/JSJD/addjsjd.vue")
const jsjdhome = () => import("../Views/JSJD/jsjdhome.vue")

const lsrw = () => import("../Views/LSRW/lsrw.vue")
const xkrw = () => import("../Views/LSRW/xkrw.vue")
const xqrw = () => import("../Views/LSRW/xqrw.vue")
const dsyrjl = () => import("../Views/LSRW/dsyrjl.vue")
const jsjdjl = () => import("../Views/LSRW/jsjdjl.vue")
const jfdh = () => import("../Views/JFDH/jifen.vue")
const jflist = () => import("../Views/JFDH/jflist.vue")
const jfdhview = () => import("../Views/JFDH/jfdhview.vue")
const addyrcg = () => import("../Views/YRCG/AddYRCG.vue")
const avater = () => import("../Views/UserInfo/other/Avater.vue")
const ZXXX = () => import("../Views/LSXX/ZXXX.vue")
const XXLB = () => import("../Views/LSXX/XXList.vue")




const routes = [
    {
        path: "/",
        name: "AuthView",
        component: AuthView
    },
    // {
    //     path: "/login",
    //     name: "AuthView",
    //     component: AuthView
    // },
    {
        path:'/cqust',
        name: '主页插件',
        component: CqustView,
        meta: { requiresAuth: true }, // 需要登录
        children: [
            {
                path: "/cqust/index",
                name: "首页",
                component: IndexView,
                meta: { title: '首页' },
            },
            {
                path: "/cqust/jsjl",
                name: "教师交流",
                component: JSJLView,
                meta: { title: '教师交流' },
            },
            {
                path: "/cqust/yrcg",
                name: "育人成果",
                component: YRCGView,
                meta: { title: '育人成果' },
            },
            {
                path: "/cqust/userinfo",
                name: "个人中心",
                component: UserInfoView,
                meta: { title: '个人中心' },
                children: [
                    {
                        path: "/cqust/userinfo/avater",
                        name: "头像修改",
                        component: avater,
                    },
                ]
            },
        ]
    },
    {
        path:'/xhome',
        name: '巡课插件',
        component: Xhome,
        children: [{
                path: "/xhome/patrol",
                name: "巡寝",
                component: AddPatrol,
                meta: { title: '巡寝记录' },
            },
            {
                path: "/xhome/xclass",
                name: "巡课",
                component: AddClass,
                meta: { title: '巡课记录' },
            }]
    },
    {
        path: "/addtalk",
        name: "发布交流",
        component: AddTalk,
        meta: { title: '发布交流' },
    },{
        path: "/addyrcg",
        name: "发布育人成果",
        component: addyrcg,
        meta: { title: '发布育人成果' },
    },
    {
        path: "/JSJLXQView",
        name: "教师交流帖子界面",
        component: JSJLXQView,
        meta: { title: '详情' },
    },
    {
        path: "/ai",
        name: "ai",
        component: AI,
        meta: { title: '重科智育云AI' },
    },
    {
        path: "/YRCGXQView",
        name: "育人成果详情",
        component: YRCGXQView,
        meta: { title: '详情' },
    },
    {
        path: "/resourceshare",
        name: "资源共享",
        component: ResourceShareView,
        meta: { title: '资源共享' },
    },
    {
        path: "/ReasourceShareXQView",
        name: "资源共享详情",
        component: ReasourceShareXQView,
        meta: { title: '资源共享详情' },
    },
    {
        path: "/ResourceShareUploaderView",
        name: "资源共享上传",
        component: ResourceShareUploaderView,
        meta: { title: '资源共享上传' },
    },
    {
        path: "/video",
        name: "视频主题",
        component: VideoView,
        meta: { title: '视频主题' },
    },
    {
        path: "/test",
        name: "岚精灵测试页面，勿删！！！",
        component: test,
        meta: { title: 'test' },
    },
    {
        path: "/testn",
        name: "nnn",
        component: testview,
        meta: { title: 'testview' },
    },
    {
        path: "/UserInfoSetting",
        name: "用户设置",
        component: UserInfoSetting,
        meta: { title: '用户设置' },
    },
    {
        path: "/classMangerView",
        name: "班级管理",
        component: ClassMangerView,
        meta: { title: '班级管理' },
    },

    {
        path: "/editor",
        name: "编辑个人信息",
        component: editor,
        meta: { title: '编辑个人信息' },
    },
    {
        path: "/MMXGView",
        name: "用户密码修改",
        component: MMXGView,
        meta: { title: '用户密码修改' },
    },
    {
        path:'/jfdh',
        name: '积分兑换模块',
        component: jfdh,
        meta: { title: '积分兑换' },
        children: [{
            path: "/jfdh/jflist",
            name: "积分列表",
            component: jflist,
            meta: { title: '积分排行榜' },
        },{
            path: "/jfdh/jfdhview",
            name: "积分兑换",
            component: jfdhview,
            meta: { title: '积分排行榜' },
        }]
    },
    {
        path:'/sjksh',
        name: '数据可视化主页',
        component: sjksh,
        children: [{
            path: "/sjksh/visualization",
            name: "可视化内嵌1",
            component: visualization,
            meta: { title: '数据可视化分析' },
        },{
            path: "/sjksh/visualization2",
            name: "可视化内嵌2",
            component: visualization2,
            meta: { title: '数据可视化分析' },
        }]
    },
    {
        path:'/rwfb',
        name: '任务发布',
        component: rwfb,
        children: [{
            path: "/rwfb/xclass",
            name: "巡课发布",
            component: xclass,
            meta: { title: '活动发布' },
        },{
            path: "/rwfb/xqs",
            name: "巡寝发布",
            component: xqs,
            meta: { title: '活动发布' },
        }]
    },
    {
        path:'/jsyr',
        name: '教师育人',
        component: jsyrhome,
        children: [{
            path: "/jsyr/addjsyr",
            name: "添加教师育人",
            component: addjsyr,
            meta: { title: '导师育人' },
        }]
    },
    {
        path:'/jsjd',
        name: '教师结队',
        component: jsjdhome,
        children: [{
            path: "/jsjd/addjsjd",
            name: "添加教师结队",
            component: addjsjd,
            meta: { title: '教师结队' },
        }]
    },
    {
        path:'/xiaoxi',
        name: '最新消息模块',
        component: ZXXX,
        meta: { title: '积分兑换' },
        children: [{
            path: "/xiaoxi/xiaoxilist",
            name: "消息部分列表",
            component: XXLB,
            meta: { title: '消息列表' },
        }]
    },
    {
        path:'/lsrw',
        name: '历史任务',
        component: lsrw,
        children: [{
            path: "/lsrw/xkrw",
            name: "巡课任务",
            component: xkrw,
            meta: { title: '历史任务' },
        },{
            path: "/lsrw/xqrw",
            name: "巡寝任务",
            component: xqrw,
            meta: { title: '历史任务' },
        },{
            path: "/lsrw/dsyrjl",
            name: "导师育人记录",
            component: dsyrjl,
            meta: { title: '历史任务' },
        },{
            path: "/lsrw/jsjdjl",
            name: "教师结队记录",
            component: jsjdjl,
            meta: { title: '历史任务' },
        }]
    },

];

const router: Router = createRouter({
    history: createWebHistory(),
    routes,
});

// 全局路由守卫
router.afterEach((to) => {
    const tabbarStore = useTabbarStore();
    tabbarStore.setActiveByPath(to.path);
});

// router.beforeEach((to) => {
//     const userStore = useUserStore()
//
//     // 公开路由白名单
//     const publicRoutes = ['/']
//     if (publicRoutes.includes(to.path)) return true
//
//     // 需要认证但未登录
//     if (!userStore.isLoggedIn) return '/'
//
//     return true
// })

router.beforeEach(async (to) => {
    const authStore = useUserStore()

    // 如果用户已登录且尝试访问登录页，重定向到首页
    if (authStore.userInfo && to.path === '/') {
        return '/cqust/index'
    }

    // 如果用户未登录且访问需要认证的页面，重定向到登录页
    if (!authStore.userInfo && to.meta.requiresAuth) {
        return '/'
    }
})


export default router;
